#!/usr/bin/env python
# -*-coding:utf-8-*-
# File    : pgnew.py
# Date    : 2021-03-25
# Author  : Tom.Lee

import re
import math
import poium


Elements = poium.Elements
CSSElement = poium.CSSElement
Browser = poium.Browser

# 通过matrix计算角度
def get_matrix(a, b, c, d,):
    a = round(180 * math.asin(a) / math.pi)
    b = round(180 * math.acos(b) / math.pi)
    c = round(180 * math.asin(c) / math.pi)
    d = round(180 * math.acos(d) / math.pi)

    if a == b or -a == b:
        deg = d
    elif -a + b == 180:
        deg = 180 + c
    elif a + b == 180:
        deg = 360 - c
    else:
        deg = 0
    return int(deg)


# 转换matrix字符串到角度
def convert_transform(matrix_str):
    args = []
    if matrix_str.startswith('matrix'):
        res = re.findall(r"matrix\((.*?)\).?", matrix_str)
        if res:
            argvs = res[0].split(',')
            for arg in argvs:
                args.append(float(arg))
            return get_matrix(*args)
    return 0


class Page(poium.Page):
    def window_scroll_to_boot(self):
        """
        滚动鼠标到页脚
        """
        js = "window.scrollTo(0, document.body.scrollHeight)"
        self.execute_script(js)

    @property
    def active_window(self):
        """
        返回当前激活的窗口
        """
        return self.driver.window_handles[-1]

    def switch_to_active_window(self):
        """
        切换到激活窗口
        :return:
        """
        self.switch_to_window(self.active_window)


class Element(poium.Element):
    def _get_element(self):
        return self._Element__get_element(self.k, self.v)

    def value_of_css_property(self, property_name):
        elem = self._get_element()
        return elem.value_of_css_property(property_name)

    def get_background_color(self):
        """
        获取元素的背景色
        """
        return self.value_of_css_property('background-color')

    def get_border_color(self):
        """
        获取元素的边框色
        """
        return self.value_of_css_property('border-color')

    def get_border_radius(self):
        """
        获取元素的边框圆角
        """
        radius = self.value_of_css_property('border-radius')
        if radius:
            return radius
        return self.value_of_css_property('-webkit-border-radius')

    def get_border_width(self):
        """
        获取元素的边框宽
        """
        return self.value_of_css_property('border-width')

    def get_border_style(self):
        """
        获取元素的边框风格
        """
        return self.value_of_css_property('border-style')

    def get_transform(self):
        """
        获取元素的旋转角度
        """
        transform_class = [
            'transform',
            '-webkit-transform',
            '-moz-transform',
            '-o-transform',
            '-ms-transform']
        for trans in transform_class:
            transform = self.value_of_css_property(trans)
            if transform:
                return convert_transform(transform)
        return 0

    def get_opacity(self):
        """
        获取元素的透明度
        """
        return self.value_of_css_property('opacity')

    def get_fontsize(self):
        """
        获取元素的字体大小
        """
        return self.value_of_css_property('font-size')

    @property
    def location(self):
        """
        返回元素坐标
        """
        elem = self._get_element()
        return elem.location
